package co.fitstart.mobile.web.controller.credit.trading;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;

import co.fitstart.entity.credit.order.CreditOrder;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CreditOrderService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile/credit-orders")
public class CreditOrderController {

    @Autowired
    private CreditOrderService creditOrderService;

    /**
     * 兑换记录
     * 
     * @param user
     * 
     * @return
     */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure User user) {

        ModelAndView modelAndView = new ModelAndView("credit/order/exchange-record-list");

        List<CreditOrder> creditOrders = creditOrderService.list(user);

        modelAndView.addObject("creditOrders", creditOrders);

        return modelAndView;
    }

    /**
     * 
     * @param user
     * @param page
     * @return
     */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    @RequestMapping(value = "", params = "filter", method = RequestMethod.GET)
    public ModelAndView listRecords(@Secure User user, Page page) {

        ModelAndView modelAndView = new ModelAndView("fragment/credit/order/exchange-record-list-fragment");

        List<CreditOrder> creditOrders = creditOrderService.list(user, page);

        modelAndView.addObject("creditOrders", creditOrders);

        return modelAndView;
    }

    /**
     * 兑换记录详情页
     * 
     * @param user
     * @return
     */
    @WechatAuthority
    @MobileDetect
    @Router(targetUrl = "/required-wechat")
    @RequestMapping(value = "/{exchangeRecordId}", method = RequestMethod.GET)
    public ModelAndView view(@Secure User user, @PathVariable("exchangeRecordId") Long exchangeRecordId) {

        ModelAndView modelAndView = new ModelAndView("credit/order/exchange-record-view");

        CreditOrder creditOrder = creditOrderService.load(exchangeRecordId);

        modelAndView.addObject("creditOrder", creditOrder);

        return modelAndView;
    }

}
